package com.atguigu.day09;

import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.table.catalog.hive.HiveCatalog;

public class Flink10_HiveCatalog {
    public static void main(String[] args) {

        //设置访问的用户
        System.setProperty("HADOOP_USER_NAME", "atguigu");

        //1.获取流的执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(1);

        //2.获取表的执行环境
        StreamTableEnvironment tableEnv = StreamTableEnvironment.create(env);

        //3.创建HiveCatalog
        String name = "MyHiveCatalog";
        String defaultDatabase = "flink_test";
        String hiveConfDir = "c:/conf";
        HiveCatalog hiveCatalog = new HiveCatalog(name, defaultDatabase, hiveConfDir);

        //4.注册hiveCatalog
        tableEnv.registerCatalog(name, hiveCatalog);

        //5.设置相关参数
        tableEnv.useCatalog(name);
        tableEnv.useDatabase(defaultDatabase);

        //6.写sql查询hive中的数据
        tableEnv.executeSql("select * from stu").print();
    }
}
